package com.zzh.bc.modules.api.controller;

import com.zzh.bc.model.ApiModel;
import com.zzh.bc.modules.api.vo.ApiVo;
import com.zzh.bc.service.api.ProjectApiService;
import com.zzh.bc.service.api.impl.ProjectApiServiceImpl;
import com.zzh.bc.utils.RequestParamUtils;
import com.zzh.bc.utils.RespParamUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;

import static com.zzh.bc.utils.Constants.*;

/**
 * 访问接口处理
 */
@WebServlet(name = "ServletApiController")
public class ServletApiController extends HttpServlet {
    private ProjectApiService apiService;

    /**
     * @param request  method = save 保存, method = update 更新， method = delete 删除。
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ApiVo vo = RequestParamUtils.requestBodyParams(request, ApiVo.class);
        if (vo == null || StringUtils.isEmpty(vo.getApiName()) || StringUtils.isEmpty(vo.getApiPattern()) ||
                StringUtils.isEmpty(vo.getApiProjectId())) {
            RespParamUtils.respJsonError(response, "参数错误");
            return;
        }
        checkService();
        switch (vo.getMethod()) {
            case METHOD_SAVE:
                boolean save = apiService.save(vo);
                if (save) {
                    RespParamUtils.respJsonOk(response, "保存成功");
                } else {
                    RespParamUtils.respJsonError(response);
                }
                return;
            case METHOD_UPDATE:
                boolean update = apiService.update(vo);
                if (update) {
                    RespParamUtils.respJsonOk(response, "保存成功");
                } else {
                    RespParamUtils.respJsonError(response);
                }
                break;
            case METHOD_QUERY:
                ApiModel model = apiService.findObjById(vo.getApiId());
                RespParamUtils.respJsonOk(response, model);
                return;
            case METHOD_DELETE:
                boolean delete = apiService.delete(vo.getApiId());
                if (delete) {
                    RespParamUtils.respJsonOk(response, "删除成功");
                } else {
                    RespParamUtils.respJsonError(response);
                }
                return;
            case METHOD_DELETE_SUB:
                boolean deleteSub = apiService.deleteParams(vo.getIds());
                if (deleteSub) {
                    RespParamUtils.respJsonOk(response, "删除成功");
                } else {
                    RespParamUtils.respJsonError(response);
                }
                return;
        }
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    void checkService() {
        if (apiService == null) {
            apiService = new ProjectApiServiceImpl();
        }
    }
}
